chrome.runtime.sendMessage(
  "cnmgcaggjdeglolimdhmidbcnohjgeia",
  {
    action: "status"
  },
  function(response)
  {
    if(!response)
    {
      chrome.management.launchApp("cnmgcaggjdeglolimdhmidbcnohjgeia");
    }
  }
)

chrome.browserAction.onClicked.addListener(function(tab) {
    // var mainPage = chrome.extension.getURL("telnet.html");
    // console.log(mainPage);
    // chrome.tabs.create({
    // url: mainPage,
    // selected: true
    // }, function(tab) {
    // });
});

function getBBSTabCode(taburl) {
  var b = taburl.split("&");
  if(b.length==2)
  {
    var mapcode = b[1].split("=");
    if(mapcode.length==2)
    {
      return mapcode[1];
    }
  }
  return 0;
}

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  var regex = new RegExp('chrome-extension:\/\/odembcomamnalolekaeejamohkjfeemj\/*', 'i');
  if(regex.test(tab.url))
  {
    if(changeInfo.status == 'complete')
    {
       //alert(tab.url);
       var nowcode = getBBSTabCode(tab.url);
       //var newTab = new bbsTabData(tab.id, );
       var wins = chrome.extension.getViews({type:'tab'});
       for (var i = 0, win; win = wins[i]; i++)
       {
        var cmdhandler = win.document.getElementById('cmdHandler');
        if(cmdhandler)
        {
          mapcode = cmdhandler.getAttribute('mapcode');
          if(mapcode && mapcode==nowcode)
          {
            cmdhandler.setAttribute('tabid', tab.id);
            win.bbsfox.tabid = tab.id;
          }
          else
          {
          }
        }
        else
        {
        }
      }
    }
  }
});
